#include "bc_weir_velocity.hpp"
#include "bc_weir_funcs.hpp"
#include "field.hpp"
#include "boundaryPatch.hpp"
#include "exceptions.hpp"
#include "formatHelpers.hpp"
#include "unifiedMathFunctions.hpp"
#include "geometry.hpp"

namespace hms
{

#define CHEAD(RETURN_TYPE) \
template<typename MeshType> \
RETURN_TYPE Weir_velocity<MeshType>::

CHEAD() Weir_velocity(
	Field<MeshType>& v,
	const BoundaryPatch& p,
	const Field<MeshType>& d,
	scalar alpha
) :
	BaseType( Type::weir_velocity, v, p, d, v, alpha )
{}

CHEAD(void) update() const {
	Field<MeshType>& v ( field() );
	const Field<MeshType>& d ( dField() );
	const BoundaryPatch& p { patch() };
	const FieldTransformer<MeshType>& transformer { this->m_transformer };

	/* this performs coordinate transformation and the computation of dEdge */
	this->updateBuffers(false);

	const auto& dDom { d.domainValues(p) };

	/* the orthogonal component is adjusted,
	 * the parallel component stays the same */
	transformer.transformed().row(0) *= min(5./3, 2*dDom/dEdge() - 1);

	v.ghostValues(p) = transformer.retransform();
}


#undef CHEAD

#define INSTANTIATE_FOR_ALL_MESHTYPES(MeshType) \
template class Weir_velocity<MeshType>;
#include "meshInstantiator.hpp"

} // namespace hms
